cap program drop tab_ctftparams
program define tab_ctftparams_indiv
        args spec
        shell mkdir -p "tablesandfigures/release/ctftparams/specs/`spec'"

        import delimited "tablesandfigures/build/ctftparams/specs/`spec'/ctftparams.csv", clear
        collapse_alpha "Individual"
        export delimited using "tablesandfigures/release/ctftparams/specs/`spec'/ctftparam_tab.csv", replace

end

program define tab_ctftparams_sg
        args spec
        shell mkdir -p "tablesandfigures/release/ctftparams/specs/`spec'"

        import delimited "tablesandfigures/build/ctftparams/specs/`spec'/ctftparams.csv", clear
        collapse_alpha "Small Group"
        export delimited using "tablesandfigures/release/ctftparams/specs/`spec'/ctftparam_tab.csv", replace
end

cap program drop collapse_alpha
program define collapse_alpha
        args market

        gen wlambda_0 = wlambda
        gen wlambda_1 = wlambda if withkids==0 & over50==0
        gen wlambda_2 = wlambda if withkids==1 & over50==0
        gen wlambda_3 = wlambda if withkids==0 & over50==1
        gen wlambda_4 = wlambda if withkids==1 & over50==1

        local collapse_spell = ""
        forval j = 0/4 {
                local var = "wlambda_`j'"
                local newspell = "(mean) mean_`var'=`var' (sd) sd_`var'=`var' (p50) p50_`var'=`var' (count) count_`var'=`var'"
                local collapse_spell = "`collapse_spell' `newspell'"

        }
        di "`collapse_spell'"
        #delimit ;
        collapse
                `collapse_spell'

        ;
        #delimit cr

        gen market = "`market'"
        reshape long mean_ sd_ p50_ count_, i(market) j(dem_var, string)
        egen sum_count = max(count_) // the total's count up top
        gen share_ = count_/sum_count if dem_var != "wlambda_0"
end

cd ../../..
global spec1 = "lowrisk_simplemh_censor0.2_main"
global spec2 = "lowrisk_simplemh_censor0.5_main"
tab_ctftparams_indiv $spec1
tab_ctftparams_indiv $spec2
global spec3 = "lowrisk_simplemh_censor0.2_switcher"
global spec4 = "lowrisk_simplemh_censor0.5_switcher"
tab_ctftparams_sg $spec3
tab_ctftparams_sg $spec4

